package jp.co.dailyReport.dailyReport.sql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import jp.co.dailyReport.Executer;

import org.apache.log4j.Logger;

/**
 * 日報作業項目情報を取得するクラスです。
 */
public class DailyReportRestTimeLoadExecuter implements Executer {

	private static Logger log = Logger.getLogger(DailyReportRestTimeLoadExecuter.class);
	private int dailyReportId;

	public DailyReportRestTimeLoadExecuter(int dailyReportId) {
		this.dailyReportId = dailyReportId;
	}

	/**
	 * 日報IDを基に、その日の休憩時間を取得します。
	 * 
	 * @param conn コネクションオブジェクト
	 * @return 休憩時間
	 * @throws SQLException
	 */
	public Object[] execute(Connection conn) throws SQLException {

		ResultSet rset = null;
		PreparedStatement pstmt = null;
		String sql = "SELECT workItem.workItemId, dailyReportWorkItem.dailyReportId, workItem.name, dailyReportWorkItem.workingHours as workingHours FROM dailyReportWorkItem LEFT JOIN workItem ON workItem.workItemId = dailyReportWorkItem.workItemId WHERE dailyReportWorkItem.dailyReportId=? and workItem.name=?";
		log.debug("sql: " + sql);
		Vector answer = new Vector();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, dailyReportId);
			pstmt.setString(2, "休憩");
			rset = pstmt.executeQuery();
			while(rset.next()) {
				Double hours = new Double(rset.getDouble("workingHours"));
				answer.add(hours);
			}
		} finally {
			if(rset != null)
			 rset.close();
			if(pstmt != null)
			 pstmt.close();
		}
		return (Double[])answer.toArray(new Double[0]);
	}
}
